package com.help10000.rms.ui.activitys.mine.user_info.password

import android.os.Bundle
import com.amir.common.utils.*
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.UpdateUserInfoPresenter
import com.help10000.rms.mvp.view.UpdateUserInfoView
import com.help10000.rms.ui.activitys.logins.LoginActivity
import com.help10000.rms.ui.base.BaseActivity
import com.help10000.rms.ui.utils.PageUtils
import kotlinx.android.synthetic.main.activity_change_pwd.*

/**
 *
 *修改密码
 */
class ChangePwdActivity : BaseActivity<UpdateUserInfoPresenter, UpdateUserInfoView>(),
    UpdateUserInfoView {


    override fun getlayoutId(): Int {
        return R.layout.activity_change_pwd
    }

    override fun initView() {

    }

    override fun initData(savedInstanceState: Bundle?) {
        et_oldpwd.requestFocus()
        IMEUtil.showIME(this, et_oldpwd)
    }

    override fun initListener() {
        btn_aff.setOnClickListener(DuplicateClickProxy {
            if (et_oldpwd.text.toString().isBlank()) {
                ToastUtil.show("旧密码不能为空")
                IMEUtil.hideIME(this, et_oldpwd)
                return@DuplicateClickProxy
            }
            if (et_newpwd.text.toString().isBlank()) {
                ToastUtil.show("新密码不能为空")
                IMEUtil.hideIME(this, et_newpwd)
                return@DuplicateClickProxy
            }
            if (et_affpwd.text.toString() != et_newpwd.text.toString()) {
                IMEUtil.hideIME(this, et_affpwd)
                ToastUtil.show("新密码和确认密码不同")
                return@DuplicateClickProxy
            }
            mPresenter.updatePwd(
                et_oldpwd.text.toString(),
                et_newpwd.text.toString()
            )
        })
    }

    override fun createPresenter(): UpdateUserInfoPresenter {
        return UpdateUserInfoPresenter(this)
    }

    override fun onSuccess() {
        ToastUtil.show("修改成功，请重新登陆")
        SPUtils.remove(Constant.AccessToken)
        SPUtils.remove(Constant.pwd)
        AppManager.getAppManager().finishAllActivity()
        PageUtils.jumpToActivity(this, LoginActivity::class.java)

    }
}